﻿# _*_ coding: utf-8 _*_
import requests
import time
import threading
import Queue
from bs4 import BeautifulSoup


urls =[
    'http://www.baidu.com',
    'http://www.taobao.com',
    'http://www.so.com'
]
start_time = time.time()
def get_url_content(q,url):
    this_start_time = time.time()
    print "线程%s开始：%s" % (threading.current_thread().name, time.time())
    r = requests.get(url).content
    soup = BeautifulSoup(r,'lxml')
    image = soup.find_all("img")
    q.put(image)
    print "线程%s结束：%s"%(threading.current_thread().name,time.time())
    this_end_time = time.time()
    print "线程%s用时：%s"%(threading.current_thread().name,this_end_time-this_start_time)
q = Queue.Queue()  #此种列表先进先出

#一般要把线程装入线程列表中，方便管理
threads = []
for url in urls:
    t = threading.Thread(target=get_url_content,args=(q,url))#创建线程
    t.start()#开启线程
    threads.append(t)#装入线程列表

for i in threads:
    i.join()#阻塞主线程（主线程速度太快，但主线程要最后结束才最好），使其运行完创建的线程（即子线程）再继续执行

while not q.empty():
    print q.get()

end_time = time.time()
print '多线程运行时间是：',end_time-start_time